var UploadWidow = function() {
	
	
	$(function() {
		
		$(document.body)
				.append(
						'<div id="basicModalContent" title="上传文件" style="display: none"><form enctype="multipart/form-data" action="/UploadServlet" id="upload-form" name="upload-form" method="post"><input type="file" name="file" /><input type="hidden" id="file_name" name="file_name" value="" /><input type="hidden" id="save_path" name="save_path" value="" /><input type="hidden" id="suffix" name="suffix" value="" /><input type="hidden" id="contentType" name="contentType" value="" /><br/><br/><input type="submit" id="btn-upload" value="确定"/></form></div>');
	});
	var beforeSubmit = function() {
		var file = $('#upload-form :file').fieldValue();
		if (file.length < 1 || (file = file[0]).length < 3)
			return false;
		var index = file.lastIndexOf('\\');
		file = file.substring(index + 1);
		index = file.lastIndexOf('.');
		if (index > -1) {
			var file_name = file.substring(0, index);
			var suffix = file.substring(index, file.length);
			$('#file_name').val(file_name);
			$('#suffix').val(suffix);
		} else {
			$('#suffix').val("nosuffix");
		}
		return true;
	};
	var success = function(data, callback, scope, params) {
		callback.call(scope || callback, data, params);
		$('#basicModalContent').remove();
	};
	return {
		show : function(save_path, callback, scope, params, url, filesize) {
		$("#basicModalContent").remove();
			$(document.body)
					.append(
							'<div id="basicModalContent" title="上传文件" style="display: none"><form enctype="multipart/form-data" action="/UploadServlet" id="upload-form" name="upload-form" method="post"><input type="file" name="file" /><input type="hidden" id="file_name" name="file_name" value="" /><input type="hidden" id="save_path" name="save_path" value="" /><input type="hidden" id="suffix" name="suffix" value="" /><input type="hidden" id="contentType" name="contentType" value="" /><br/>(注:文件格式限定下面几种*.doc,*.docx,*.xls,*.xlsx,*.txt,*.ppt,*.pptx,*.pdf,*.zip,<br/>*.rar,*.png,*.gif,*.bmp,*.jpg,*.jpeg,*.eml,*.rtf<br/>文件大小不超过'+filesize+'MB)<br/><input type="submit" id="btn-upload" value="确定"/></form></div>');
			if (url) {
				$('#upload-form').attr('action', url);
			}
			$('#basicModalContent').dialog( {
				modal : true,
				width: 380
			});
			$('#save_path').val(save_path);
			$('#upload-form').submit(function() {
				if ($('#upload-form :file').val() == "") {
					alert("请选择要上传的文件!");
					return false;
				}
				$(this).ajaxSubmit( {
					dataType : 'json',
					type : 'post',
					beforeSubmit : beforeSubmit,
					success : function(data) {
						success(data, callback, scope, params);
					}
				});
				return false;
			});
		}
	}
}();

function getFileType(filename) {
	var filetype = filename.split('.')[filename.split('.').length - 1];
	return getFileTypeByExtend(filetype.toLowerCase());
	
}

function getFileTypeByExtend(filetype) {
	switch(filetype)
	   {
			case "doc":
				return '1';
				break;
			case "xls":
				return '2';
				break;
			case "txt":
				return '3';
				break;
			case "ppt":
				return '4';
				break;
			case "pdf":
				return '5';
				break;
			case "zip":
				return '6';
				break;
			case "rar":
				return '7';
				break;
			case "png":
				return '8';
				break;
			case "gif":
				return '9';
				break;
			case "bmp":
				return '10';
				break;
			case "jpg":
				return '11';
				break;
			case "jpeg":
				return '12';
				break;
			case "eml":
				return '13';
				break;
			case "rtf":
				return '14';
				break;
			case "docx":
				return '15';
				break;
			case "xlsx":
				return '16';
				break;
			case "pptx":
				return '17';
				break;
			case "tif":
				return '18';
				break;
			default:
				return '-1';
		}
}